
from flask import Blueprint,request,jsonify
from models import db
from models.user import *

users_blueprint = Blueprint('users_blueprint',__name__)

@users_blueprint.route('/roles',methods=['GET',"POST"])
def roles():
    if request.method == "GET":
        rolelist = Role.query.all()
        rlist =[{"id":i.id,'name':i.name} for i in rolelist]
        return jsonify({"code":"1","msg":rlist})
    else:
        #添加
        role = Role(name='zdming')
        db.session.add(role)
        db.session.commit()
        return jsonify({"code":"1","msg":"ok"})
    
@users_blueprint.route('/users',methods=['GET',"POST"])
def users():
    if request.method == "GET":
        #查询用户对应的角色
        # #查询角色下有哪些用户
        rolenames = Role.query.filter_by(id=1).first()
        print(rolenames.us)
        rolelist = User.query.all()
        rlist =[{"id":i.id,'username':i.username,'roleid':i.role_id} for i in rolelist]
        return jsonify({"code":"1","msg":rlist})
    else:
        #添加
        role = User(username='stu123',password='123456789',email='123456789@qq.com',role_id=1)
        db.session.add(role)
        db.session.commit()
        return jsonify({"code":"1","msg":"ok"})
    

@users_blueprint.route('/resouces',methods=['GET',"POST"])
def resouces():
    if request.method == "GET":
        role=Role.query.filter_by(id=6).first()
        print(role.resource)
        return jsonify({"code":"1","msg":"rlist"})
    else:
        #添加
        res1 = Resouce(name='数据111',pid=None)
        res2 = Resouce(name='资产121',pid=None)
        db.session.add_all([res1, res2])
        
        # role= Role(name='ta134234')
        # role.resource=[res1,res2]
       
        # db.session.add(role)
        db.session.commit()
        return jsonify({"code":"1","msg":"ok"})